package com.sun.controller;

import com.sun.myexception.MyException;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class EXceptionTest {

    @RequestMapping("/errorTest1")
    public String errorTest1(Integer i){
        System.out.println("errorTest1");
        System.out.println("test1:10/"+i.toString()+" = "+ 10/i);
        return "success";
    }

    @RequestMapping("/user")
    public String errorTest02(@RequestParam(value = "username") String user) {
        if ("admin".equals(user)) {
            System.out.println("username="+user+" is ok");
            return "success";
        } else {
            System.out.println("username =admin");
            throw new MyException();
        }
    }

    //加入i=0,会使用此方法
    @ExceptionHandler(value = {ArithmeticException.class})
    public String handleException01(Exception e ,Model model){
        System.out.println("handleException01:"+e.getMessage());
        model.addAttribute("errorMsg","handleException01:"+e);
        return "error";
    }


   /* @ExceptionHandler(value = {Exception.class})
    public String handleException02(Exception e ,Model model){
        System.out.println("handleException02:"+e.getMessage());
        model.addAttribute("errorMsg","handleException02:"+e);
        return "error";
    }*/
}
